﻿@{
    ViewData["Title"] = "Update";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑供应商</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background-color: #f9f9f9;
        }

        .container {
            background-color: white;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ddd;
            width: 80%;
            margin: auto;
        }

        .header {
            font-size: 1.2em;
            background-color: lightgray;
            padding: 10px;
            border-bottom: 1px solid #ccc;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }

        th {
            background-color: #f4f4f4;
            font-weight: bold;
            width: 150px;
        }

        input[type="text"], input[type="number"], select {
            width: calc(100% - 22px);
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        .button-container {
            text-align: right;
            margin-top: 10px;
        }

        .button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 5px;
            margin: 5px;
        }

            .button:hover {
                background-color: #0056b3;
            }
    </style>
</head>
<body>

    <div class="container">
        <form id="stockForm" onsubmit="submitData(); return false;">
            <table>
                <tr>
                    <td>
                        <label for="Name">盘点人</label>
                        <input type="text" id="Name" required placeholder="请输入盘点人">
                    </td>
                    <td>
                        <label for="Product">盘点商品</label>
                        <select id="Product" required>
                            <option value="">请选择商品</option>
                            <!-- 动态加载商品选项 -->
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="bookQuantity">账面数量</label>
                        <input type="number" id="bookQuantity" required placeholder="请输入账面数量">
                    </td>
                    <td>
                        <label for="sdate">状态</label>
                        <input type="radio" name="sdate" value="1" checked> 正常
                        <input type="radio" name="sdate" value="2"> 异常
                    </td>
                </tr>
            </table>

            <div class="buttons">
                <button type="submit">确定</button>
                <button type="button" onclick="reset()">关闭</button>
            </div>
        </form>
    </div>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.all.js"></script>

    <script>
        $(function () {
            var a=0;
            loadProducts();
            GetVendor();
        });

        // 加载商品数据
        function loadProducts() {
            $.ajax({
                url: '/TakeStock/GetProductDetail', // 请替换为你的API地址
                type: 'GET',
                async:false,
                success: function (response) {
                    var productSelect = $('#Product');
                    $.each(response.data, function (index, product) {
                        productSelect.append('<option value="' + product.productName + '">' + product.productName + '</option>');
                    });
                    layui.form.render("select");
                },
                error: function (xhr, status, error) {
                    console.error('加载商品数据失败: ' + error);
                }
            });
        }

        function GetVendor() {
            var Id = @ViewBag.Id;
            $.ajax({
                url: '/TakeStock/BackfillData',
                type: 'GET',
                async:true,
                data: { Id: Id },
                success: function (res) {
                    console.log(res);
                    $('#Name').val(res.data.name);
                    $('#Product').val(res.data.good); // 假设的字段
                    layui.form.render("select");
                    $('#bookQuantity').val(res.data.bookQuantity);
                    $('input[name="sdate"][value="' + res.data.status + '"]').prop('checked', true);
                    a=res.data.countQuantity;
                }
            });
        }

        function submitData() {
            var ID = @ViewBag.Id;

            var data = {
                Id: ID,
                name: $('#Name').val(),
                good: $('#Product').val(),
                bookQuantity: parseInt($('#bookQuantity').val()),
                sdate: parseInt($('input[name="sdate"]:checked').val()),
                countQuantity:a
            };

            $.ajax({
                url: '/TakeStock/UpdateTakeStock',
                type: 'POST',
                data: data,
                success: function (res) {
                    if (res == 1) {
                        layer.msg('修改成功', { icon: 1, time: 1000 }, function() {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg('修改失败: ' + res.msg, { icon: 2, time: 2000 });
                    }
                }
            });
        }

        function reset() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    </script>

</body>
</html>
